package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.entity.Hist;
import com.github.pagehelper.Page;
import com.service.HistService;

@RestController //定义为控制器 返回JSON类型数据
@RequestMapping(value = "/hist", produces = "application/json; charset=utf-8")// 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class HistController { // extends BaseController {
	// TODO Auto-generated method stub

	@Autowired // @Autowired的作用是自动注入依赖的ServiceBean
	private HistService histService;

	// 预处理 获取基础参数
	@GetMapping(value = "createHist.action")
	public Map<String, Object> createHist() {
		Map<String, Object> map = new HashMap<String, Object>();
		return map;
	}

	// 按主键删除一个浏览记录
	@GetMapping(value = "deleteHist.action")
	public Map<String, Object> deleteHist(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int num = this.histService.deleteHist(id);
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "删除成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "删除失败");
		}
		return map;
	}

	// 按主键批量删除浏览记录
	@PostMapping(value = "deleteHistByIds.action")
	public Map<String, Object> deleteHistByIds(@RequestBody String[] ids) {
		int num = 0;
		for (String histid : ids) {
			num += this.histService.deleteHist(histid);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "删除成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "删除失败");
		}
		return map;
	}

	// 修改浏览记录
	@PostMapping(value = "updateHist.action")
	public Map<String, Object> updateHist(@RequestBody String jsonStr) {
		JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
		Hist hist = this.histService.getHistById(obj.getString("histid")); // 获取object中histid字段
		hist.setUsersid(obj.getString("usersid")); //  为用户赋值
		hist.setGoodsid(obj.getString("goodsid")); //  为商品赋值
		hist.setNum(obj.getString("num")); //  为浏览数赋值

		Map<String, Object> map = new HashMap<String, Object>();
		int num = this.histService.updateHist(hist);
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "修改成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "修改失败");
		}
		return map;
	}

	// 查询全部浏览记录数据 在下拉菜单中显示
	@GetMapping(value = "getAllHist.action")
	public List<Hist> getAllHist() {
		return this.histService.getAllHist();
	}

	// 按关键字查询浏览记录数据 在下拉菜单中显示
	@GetMapping(value = "getHistMap.action")
	public Map<String, Object> getHistMap(String keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		Hist hist = new Hist();
		hist.setUsersid(keywords);
		List<Hist> list = this.histService.getHistByLike(hist);
		map.put("data", list);
		return map;
	}

	// 通过AJAX在表格中显示浏览记录数据
	@GetMapping(value = "getHistByPage.action")
	public Map<String, Object> getHistByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Hist> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		List<Hist> list = this.histService.getAllHist();
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示浏览记录数据
	@GetMapping(value = "getHist.action")
	public Map<String, Object> getHist(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String keywords) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Hist> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Hist hist = new Hist();
		hist.setUsersid(keywords);
		List<Hist> list = this.histService.getHistByLike(hist);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示浏览记录数据
	@GetMapping(value = "getUserHist.action")
	public Map<String, Object> getUserHist(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit, String id) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Hist> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Hist hist = new Hist();
		hist.setUsersid(id);
		List<Hist> list = this.histService.getHistByLike(hist);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示浏览记录数据
	@GetMapping(value = "getOwnerHist.action")
	public Map<String, Object> getOwnerHist(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit, String id) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Hist> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Hist hist = new Hist();
		//hist.setAdminid(id);
		List<Hist> list = this.histService.getHistByLike(hist);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 按主键查询浏览记录数据
	@GetMapping(value = "getHistById.action")
	public Hist getHistById(String id) {
		Hist hist = this.histService.getHistById(id);
		return hist;
	}

	// TODO Auto-generated method stub
}
//


